package cn.tedu.v6;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;

public class Client {
    private Socket socket;
    private String ip;
    private static final String HOST="172.20.17.9";
    private static final Integer PORT=6666;
    private PrintWriter pw;
    private BufferedReader br;

    public Client() {
        try {

            socket=new Socket(HOST,PORT);
            br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
            pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
        } catch (NullPointerException e){
            System.out.println("友情提示：IP传入错误导致链接失败~");
        } catch (UnknownHostException e){
            System.out.println("友情提示：IP传入错误");
        } catch (ConnectException e){
            System.out.println("客户端链接失败：建议检查IP或者端口号");
        } catch (IOException e) {
            System.out.println("客户端链接服务器失败，提示：检查IP/端口");
        }
        ip=socket.getInetAddress().getHostAddress();
    }

    private void chat(){
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("服务器说："+message);
                    System.out.println("客户端请输入(输入exit退出)：");
                    //服务器端广播之后，提示客户端请输入(输入exit退出)：
                }
            } catch (IOException e) {
                System.out.println("服务器端读入失败");
            }
        }).start();


        Scanner input = new Scanner(System.in);
        System.out.println("客户端请输入：");
        while (true) {
            String line = input.nextLine();
            //如果客户端输入exit（不考虑大小写），就退出聊天
            if(line.equalsIgnoreCase("exit")) {
                pw.println("客户端断开连接，ip是:"+ip);//发送给服务器端说一声
                System.out.println("退出聊天...拜拜~~~");
                System.exit(0);  //终止运行
            }else{ //如果不是exit，就发送给服务器端
                pw.println(line);
            }
        }
    }

    public void start(){
        System.out.println("~~欢迎使用昭宇牌QQ聊天室~~");
        Scanner input=new Scanner(System.in);
        try {

            String serverMessage=br.readLine();
            System.out.println(serverMessage);
            String username=input.nextLine();
            pw.println(username);

            serverMessage=br.readLine();
            System.out.println(serverMessage);

            String password=input.nextLine();
            pw.println(password);
            serverMessage=br.readLine();

            if(serverMessage.startsWith("登录成功")){
                chat();
            }
        } catch (IOException e){
            System.out.println("客户端断开连接了！");
        } finally {
            if(br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败~");
                }
            if(pw!=null)
                pw.close();
            if(socket!=null&&!socket.isClosed())
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("通讯关闭失败~");
                }
        }
    }



    public static void main(String[] args){
        new Client().start();
    }
}
